package main

import (
	"fmt"
	"github.com/Shopify/sarama"
	"log"
	"time"
)

func main() {

	//1.生产者配置
	config := sarama.NewConfig()
	config.Producer.RequiredAcks = sarama.WaitForAll          //等待leader和follow确认
	config.Producer.Timeout = 30 * time.Second                //超时时间
	config.Producer.Partitioner = sarama.NewRandomPartitioner //分区
	config.Producer.Return.Successes = true                   //确认
	//2.连接kafka
	cli, err := sarama.NewSyncProducer([]string{":9092"}, config)
	if err != nil {
		log.Fatalln(err)
	}
	defer cli.Close()
	//3.封装消息
	msg := &sarama.ProducerMessage{
		Topic: "web_log",
		Value: sarama.StringEncoder("看一个孤单的背影"),
	}

	//4、往kafka发送消息
	pid, offset, err := cli.SendMessage(msg)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("pid:", pid, " offset:", offset)

}
